# _*_ coding : utf-8 _*_
# @Time: 2024/7/16 17:00
# @Author : 石文帅	
# @File : ExcelData
# @Project : python-interface-automation
import os

import pandas


class ExcelData(object):
    '''Excel的读取并返回'''

    def __init__(self, filename):
        base_path = os.path.dirname(os.path.dirname(__file__))  # 项目路径
        data_path = os.path.join(base_path, 'Data')  # 数据路径
        file_path = os.path.join(data_path, filename)  # 文件路径
        # 判断文件名是csv还是Excel
        if filename.split('.')[1] == 'csv':
            self.file = pandas.read_csv(file_path, keep_default_na=False)  # 读取csv文件
        elif filename.split('.')[1] == 'xls' or filename.split('.')[1] == 'xlsx':
            self.file = pandas.read_excel(file_path, keep_default_na=False)
        else:
            print("不支持的文件格式")

    def get_values_to_list(self):
        """将文件数据读取成列表嵌套列表格式"""
        return self.file.values.tolist()
        pass

    def get_values_to_dict(self):
        """将文件数据读取成列表嵌套字典格式"""
        return [self.file.loc[i].to_dict() for i in self.file.index.values]